<template>
  <el-dialog
    class="met-dialog"
    title="选择模板"
    width="950px"
    :visible.sync="dialog.visible"
    :append-to-body="true"
  >
    <!-- 添加 会议室-->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      class="header"
    >
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          class="w150"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >

        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          multiple
          :show-file-list="false"
          :on-success="successubjectAttachments"
          :headers="headers"
        >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >上传模板</el-button
          >
        </el-upload>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="roleList"
      highlight-current-row
      @selection-change="handleSelectionChange"
      height="500"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="模板名称" prop="templateName" />
      <el-table-column
        label="模板类型"
        prop="templateType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope"> 合同模板 </template>
      </el-table-column>
      <el-table-column
        label="上传时间"
        prop="createTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" prop="templateType">
        <template slot-scope="scope">
          <el-button type="text" @click="ondel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div class="foot">
      <el-button type="primary" @click="submitForm('queryForm')"
        >确定</el-button
      >
      <el-button @click="resetForm('queryForm')">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";
import moment from "moment";
import { listRole, delRole } from "@/api/system/role";
import { agreelist, userlistpage, agreeadd, agreedel } from "@/api/case/case";
import { download } from "@/api/case/case";
export default {
  components: {},
  dicts: [
    "customer_state",
    "approval_status",
    "category_id",
    "unit",
    "person",
    "module_type",
  ],
  data() {
    return {
      dialog: {
        visible: false,
        title: "客户选择",
      },
      options: [{ value: 1, label: "正常" }],
      roleList: [], //表格数组
      total: 1,
      uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      loading: false,
      headers: { Authorization: "Bearer " + getToken() },
      current: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateName: undefined,
      },
    };
  },
  computed: {},
  methods: {
    //打开弹窗
    async open() {
      this.getList();
      this.current = [];
      this.dialog.visible = true;
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      agreelist(this.queryParams)
        .then((response) => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((err) => {
          // this.loading = false;
        });
    },
    successubjectAttachments(response, file, fileList) {
      //标的额证明文件
      console.log("file");
      console.log(file);
      if (response.code == 200) {
        this.loading = true;
        let that = this;
        let types = file.response?.newFileName?.split(".") || [];
        let type = types[types.length - 1];
        let param = {
          mimeType: type || file.raw.type,
          size: file.size,
          templateName: response.originalFilename,
          templateType: type || file.raw.type,
          url: response.url,
          updateTime: moment().format("yyyy-MM-DD HH:mm:ss"),
          createTime: moment().format("yyyy-MM-DD HH:mm:ss"),
        };
        agreeadd(param).then((res) => {
          console.log(res);
          that.$message.success("添加成功！");
          that.getList();
        });
      } else {
        this.$message.error(response.msg);
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.roleList = [];
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    // //选择客户
    handleSelectionChange(val) {
      console.log(val);
      this.current = val;
    },
    ondel(row) {
      let that = this;
      this.$modal
        .confirm("是否确认删除" + row.templateName + '"的文件模板？')
        .then(() =>{
          that.loading = true;
          agreedel(row.id).then((res) => {
            that.$message.success("删除成功");
            that.getList();
          });
        })
        .catch(() => {});
    },
    submitForm(formName) {
      if (this.current.length != 0) {
        this.loading = true;
        this.current.map((item, index) => {
          let resource = `${item.url}`;
          this.$download.resource(resource);
          if (index == this.current.length - 1) {
            this.resetForm("queryForm");
            this.$message.success("下载成功！");
            console.log("提交成功");
            this.$emit("submit", this.current);
            this.dialog.visible = false;
          }
        });
      } else {
        this.$message.warning("请先选择数据！");
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialog.visible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.foot {
  text-align: center;
  margin-top: 50px;
}
.header {
  margin: 20px;
}
::v-deep .w150.el-input.el-input--small.el-input--suffix {
  width: 150px;
}
::v-deep .el-select.w150.el-select--small {
  width: 150px;
}
::v-deep .pagination-container .el-pagination {
  position: relative;
  left: 0;
  right: 0;
  text-align: center;
}
::v-deep .el-dialog__body {
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  padding: 20px;
}
::v-deep .el-table__body-wrapper.is-scrolling-none {
  min-height: 130px !important;
  height: calc(100vh - 570px) !important;
}
.upload-demo {
  float: right;
  margin-left: 10px;
}
</style>
